import Vue from "vue";
import VueRouter from "vue-router";
import VueCookies from "vue-cookies";
import { Message } from "element-ui";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "banner",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/admin/index.vue"),
      children: [
        {
          path: "/banner",
          name: "banner-manager",
          component: () => import("../views/admin/components/banner-manager.vue"),
          meta: {
            needToken: false,
          },
        },
      ]
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  // 定义一个白名单列表, 里面有登录页,注册页,也可以写其他页面
  if (to.meta.needToken) {
    if (VueCookies.get("adminToken")) {
      next();
    } else {
      Message.error("请先登录管理员账号");
      next({
        path: "/",
        replace: true,
      });
    }
  } else {
    next();
  }
});

export default router;
